<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="copyright" content="留学网">
<meta name="author" content="留学网">
<title>留学网</title>
<link rel="stylesheet" href="./static/css/element-ui.css">
<script src="./static/js/basic.js"></script>
<script type="text/javascript" src="./static/js/swiper-4.5.3.min.js"></script>
<link rel="stylesheet" href="./swiper/swiper.min.css" />
<script src="./static/js/element-ui.js"></script>
<script src="./static/js/js.js"></script>
<script src="./static/js/component.js"></script>
</head>

<body>
  <div v-cloak id="app" class="curNav3">
    <layout>
      <div v-if="info" class="my">
        <img :src="info.avatar" class="avatar" alt="">
        <div class="name">{{info.nickname}}</div>
      </div>
    </layout>
  </div>
  <script>
    
  </script>
  <script type="text/babel">
    var time = new Date().getTime()
    new Vue({
      el: '#app',
      data:{
        info:null,
        
      },
      created() {
          
        // jump()
      },
      mounted() {

        this.getList()
      },
      computed:{
        total_price(){
          let price = 0;
          for(let i = 0; i < this.cartList.length; i++){
            price = price + Number(this.cartList[i].total_price);
          }
          console.log('price-----------------',  price)
          return price.toFixed(2)
        }
      },
      methods: {
        
        getList(){
          getUserInfoApi({}, res=>{
            this.info = res;
          })
        },
        
        add(item){
          console.log('aaa--------', item.number, item.number === 0);
          const _num = item.number === 0 ? item.selling_quantity : item.number+1;
          console.log('bbb---------', _num, item.number)
          this.getCartList(item.id, _num)
          
        },
        jian(item){
          const _num = item.number === item.selling_quantity ? 0 : item.number-1;
          if(_num == 0){
            const index = this.list.findIndex( n => n.id === item.id);
            this.list[index].number = 0;
            this.list[index].total_price = '0.00';
            this.cartList = this.list.filter( n => n.number > 0)
          }else{
            this.getCartList(item.id, _num)
          }
          
          
        },
        
        
        
        // 购物车加减
        clearcart(){
          for(let i = 0; i < this.list.length; i++){
            this.list[i].number = 0;
            this.list[i].total_price = '0.00'
          }
          this.cartList = [];
        },
        getCartList(id, _num){
          console.log('_num----', _num)
          const index = this.list.findIndex( n => n.id === id);
          getProductStockApi({product_id: id, number: _num },res=>{
            this.list[index].number = _num;
            this.list[index].total_price = res.price;
            this.cartList = this.list.filter( n => n.number > 0)
          })
        },
        
        
        
        getPrice(item){
          return (Number(item.price) * item.number).toFixed(2)
        },
      }
    })
  </script>
  
  <style>
    
  </style>
  
</body>
</html>